package com.lzy.system.organization.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.conf.I18nServiceImpl;
import com.lzy.exception.BusException;
import com.lzy.system.organization.dto.SysResDTO;
import com.lzy.system.organization.entity.SysRes;
import com.lzy.system.organization.entity.SysResRole;
import com.lzy.system.organization.mapper.SysResMapper;
import com.lzy.system.organization.service.ISysResRoleService;
import com.lzy.system.organization.service.ISysResService;
import com.lzy.utils.BeanMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2022-08-17
 */
@Service
public class SysResServiceImpl extends ServiceImpl<SysResMapper, SysRes> implements ISysResService {
    I18nServiceImpl i18nService;
    ISysResRoleService iSysResRoleService;

    @Autowired
    public SysResServiceImpl(I18nServiceImpl i18nService, ISysResRoleService iSysResRoleService) {
        this.i18nService = i18nService;
        this.iSysResRoleService = iSysResRoleService;
    }

    @Override
    public List<SysRes> getList() throws BusException {
        QueryWrapper<SysRes> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("id");
        List<SysRes> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SysRes> getListDir() throws BusException {
        QueryWrapper<SysRes> queryWrapper = new QueryWrapper();
        queryWrapper.eq("res_type",1);
        queryWrapper.orderByAsc("id");
        List<SysRes> list = this.list(queryWrapper);
        list.add(new SysRes(){{
            setId(0L);
            setPid(-1L);
            setResName("根目录");
        }});
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(SysResDTO sysResDTO) throws BusException {
        SysRes sysRes = (SysRes) BeanMapHelper.Bean2Bean(sysResDTO, SysRes.class);
        if (sysRes.getRes_type() == 1) {
            sysRes.setLink("");
        } else if (sysRes.getRes_type() == 2) {
            if (StrUtil.isBlank(sysRes.getLink())) {
                throw new BusException(i18nService.getMessage("SysRes.link.save.error"));
            }
        }
        if (sysRes.getPid() == null) {
            sysRes.setPid(0L);
        }
        sysRes.setCreateDate(new Date());
        return this.save(sysRes);
    }

    @Override
    public boolean update(SysResDTO sysResDTO) throws BusException {
        SysRes sysRes = (SysRes) BeanMapHelper.Bean2Bean(sysResDTO, SysRes.class);
        return this.updateById(sysRes);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delById(long id) throws BusException {
        QueryWrapper<SysRes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusException("当前资源有下级资源不能删除");
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delBatch(List<SysRes> list) throws BusException {
        List<SysRes> links = list.stream().filter(item -> item.getRes_type() == 2).collect(Collectors.toList());
        List<SysRes> dics = list.stream().filter(item -> item.getRes_type() == 1).collect(Collectors.toList());

        List<Long> link_ids = new ArrayList<>();
        List<Long> dic_ids = new ArrayList<>();
        for (SysRes link : links) {
            link_ids.add(link.getId());
        }

        this.removeBatchByIds(link_ids);

        //查询对于的目录下是否还有链接
        for (SysRes dic : dics) {
            QueryWrapper<SysRes> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",dic.getId());
            long count = this.count(queryWrapper);
            if(count == 0){
                dic_ids.add(dic.getId());
            }
        }
        this.removeBatchByIds(dic_ids);

        //级联删除资源对于的权限
        for (SysRes link : links) {
            QueryWrapper<SysResRole> queryWrapper = new QueryWrapper();
            queryWrapper.eq("RESID",link.getId());
            iSysResRoleService.remove(queryWrapper);
        }
        return true;
    }


}
